﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using BasicModuleOrm.Model;
using Dapper;

namespace BasicModuleOrm.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ProductDapperController : ControllerBase
    {
        private DapperContext _dapperContext;

        public ProductDapperController(DapperContext dapperContext)
        {
            _dapperContext = dapperContext;
        }

        /// <summary>
        /// 查询所有商品
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<IEnumerable<Product>> GetProducts()
        {
            //1、创建连接
            using (var connection = _dapperContext.CreateConnection())
            {
                // 2、查询商品
                var products = connection.Query<Product>("select * from product").ToList();

                // 3、返回商品
                return products;
            }
        }

        /// <summary>
        /// 查询商品根据商品Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public ActionResult<Product> GetProduct(int id)
        {
            //1、创建连接
            using (var connection = _dapperContext.CreateConnection())
            {
                // 2、查询商品
                var product = connection.QuerySingleOrDefault<Product>("select * from product where Id=@Id", new { Id = id });

                // 3、返回商品
                return product;
            }
        }

        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<Product> PostProduct(Product product)
        {
            //1、创建连接
            using (var connection = _dapperContext.CreateConnection())
            {
                // 2、添加商品
                connection.Execute("insert into product (Name, Price, Stock) values (@Name,@Price,@Stock)", product);

                // 3、返回商品
                return CreatedAtAction(nameof(PostProduct), new { id = product.Id }, product);
            }
        }

        /// <summary>
        /// 更新商品通过Id
        /// </summary>
        /// <param name="id"></param>
        /// <param name="product"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public IActionResult PutProduct(int id, Product product)
        {
            //1、创建连接
            using (var connection = _dapperContext.CreateConnection())
            {
                // 2、更新商品
                connection.Execute("update product set Name = @Name, Price = @Price, Stock = @Stock where Id = @Id", product);

                // 3、返回商品
                return Ok("更新成功");
            }
        }

        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public IActionResult DeleteProduct(int id)
        {
            //1、创建连接
            using (var connection = _dapperContext.CreateConnection())
            {
                // 2、删除商品
                connection.Execute("delete from product where Id = @Id", new { Id = id });

                return Ok("删除成功");
            }
        }
    }
}
